package com.plusjun.seckill.scheduled;

import com.plusjun.common.constant.SecKillConstant;
import com.plusjun.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SeckillSkuScheduled {

    @Resource
    SeckillService seckillService;

    @Resource
    private Redisson redisson;

    @Scheduled(cron = "0 * * * * ?")
    public void upSecKillSkuLatest3Days() {
        // 1、处理重复提交
        RLock lock = redisson.getLock(SecKillConstant.UPLOAD_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            log.info("上架秒杀的商品~~~~~~~~~~");
            seckillService.upSecKillSkuLatest3Days();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}
